<template>
    <div class="flow-name-panel">
        <div class="name-edit-container" @click="enterEditMode">
            <span class="name-title">应用名：</span>
            <input ref="inputRef" v-model="flowName" :readonly="!isEditingName" class="name-input" @blur="saveFlowName"
                @keyup.enter="saveFlowName" />
            <el-button v-if="isEditingName" circle size="small" @click.stop="saveFlowName" @dbclick="enterEditMode"
                class="edit-name-button">
                <i class="el-icon-check"></i>
            </el-button>
            <el-button v-else circle size="small" @click.stop="enterEditMode" class="edit-name-button">
                <Edit />
            </el-button>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, nextTick, computed } from 'vue'
import { Edit } from '@icon-park/vue-next';

// Props & Emits
const props = defineProps<{
    modelValue?: string
}>()

const emit = defineEmits<{
    (e: 'update:modelValue', value: string): void
}>()

// 数据绑定
const flowName = computed({
    get: () => {
        if (props.modelValue === undefined || props.modelValue === null) {
            return '未命名流程'
        }
        return props.modelValue
    },
    set: (value) => emit('update:modelValue', value)
})

const isEditingName = ref(false)
const inputRef = ref<HTMLInputElement | null>(null)

// 进入编辑模式
const enterEditMode = () => {
    isEditingName.value = true
    nextTick(() => {
        const el = inputRef.value
        if (el) {
            el.readOnly = false
            el.focus()
            el.select() // 自动选中内容，提升体验
        }
    })
}

// 保存并退出编辑
const saveFlowName = () => {
    isEditingName.value = false
    const el = inputRef.value
    if (el) {
        el.readOnly = true
    }
    const newName = flowName.value.trim()
    emit('update:modelValue', newName === '未命名流程' ? '' : newName)
}
</script>

<style scoped>
.flow-name-panel {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 8px;
    background-color: #f5f7fa;
    height: 28px;
    border-radius: 4px;
}

.name-edit-container {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
}

.name-title {
    font-size: 14px;
}

.name-input {
    font-size: 14px;
    color: #333;
    line-height: 20px;
    height: 20px;
    padding: 4px 6px;
    box-sizing: border-box;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 160px;
    width: 100%;
    background-color: transparent;
    border: none;
    outline: none;
    transition: all 0.2s ease;
    cursor: pointer;
}

.name-input[readonly] {
    cursor: pointer;
}

.name-input:focus,
.name-input:hover:not([readonly]) {
    outline: none;
    background-color: #ecf5ff;
    border: 1px solid #409EFF;
    border-radius: 4px;
}

.edit-name-button {
    padding: 4px;
    font-size: 12px;
    background-color: #fff;
    color: #999;
    border: 1px solid #ddd;
    cursor: pointer;
}

.edit-name-button:hover {
    color: #42b983;
    border-color: #42b983;
}
</style>